﻿Ext.define('app.prm.product.Document', {
    extend: 'Ext.grid.Panel',
    border: false,
    enableLocking: true,
    viewConfig: {
        enableTextSelection: true //可以复制单元格文字
    },
    forceFit: false,
    columns: [],
    title: '项目文档',
    columnLines: true,
    productId: 0,
    initComponent: function () {
        var me = this;
        var column = [];
        column.push({ header: '客户', dataIndex: 'customer', locked: true });
        column.push({ header: '项目名称', dataIndex: 'projectName', locked: true });
        column.push({ header: '项目号', dataIndex: 'projectNo', locked: true });

        App.AjaxGet('/api/services/app/document/GetDocumentReportColumn', function (data) {
            var result = data.result;
            for (var i = 0; i < result.length; i++) {
                var root = result[i];
                var rootOjb = { header: root.text };
                if (root.leaf) {
                    rootOjb.dataIndex = root.text + root.id;
                    rootOjb.docTypeId = root.id;
                    rootOjb.renderer = function (value, p, record) {
                        if (value == undefined || value == '')
                        {
                            return '';
                        }
                        var v = value.split('/')[0];
                        if (v > 0) {
                            return '<span style="color:blue">' + value + '</span>'
                        }
                        return value;
                    }
                }
                else {
                    rootOjb.columns = [];
                    for (var j = 0; j < root.children.length; j++) {
                        var record = root.children[j];
                        var twoOjb = { header: record.text };
                        if (record.leaf) {
                            twoOjb.dataIndex = record.text + record.id;
                            twoOjb.docTypeId = record.id;
                            twoOjb.renderer = function (value, p, record) {
                                if (value == undefined || value == '') {
                                    return '';
                                }
                                var v = value.split('/')[0];
                                if (v > 0) {
                                    return '<span style="color:blue">' + value + '</span>'
                                }
                                return value;
                            }
                        }
                        else {
                            twoOjb.columns = [];
                            for (var k = 0; k < record.children.length; k++) {
                                var last = record.children[k];
                                var lastObj = {
                                    header: last.text, docTypeId: last.id, dataIndex: last.text + last.id, align: 'center',
                                    renderer: function (value, p, record) {
                                        if (value == undefined || value == '') {
                                            return '';
                                        }
                                        var v = value.split('/')[0];
                                        if (v > 0) { 
                                            return '<span style="color:blue">' + value + '</span>'
                                        }
                                        return value;
                                    }
                                };
                                twoOjb.columns.push(lastObj);
                            }
                        }
                        rootOjb.columns.push(twoOjb);
                    }
                }
                column.push(rootOjb);
            }
            var store = Ext.create("app.ux.PrmStore", { url: '/api/services/app/project/GetProjectDocumentReport?productId=' + me.productId, remoteSort: false });
            me.reconfigure(store, column);
        });
        me.callParent();
    },
    listeners: {
        celldblclick: 'onOpenDocument'
    }
});